package com.cqh.recruit.controller.school;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.recruit.pojo.dto.MEmployeeQueryDto;
import com.cqh.recruit.pojo.entity.Employee;
import com.cqh.recruit.pojo.vo.MEmployeeListVo;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IEmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 *
 *  前端控制器
 *
 *
 * @author dh
 * @since 2025-10-18
 * @version v1.0
 */
@RestController
@RequestMapping("/api/school/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/find")
    @Operation(summary = "根据id查询员工")
    public Result find(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        Employee employee = employeeService.getById(id);
        return Result.buildSuccess(employee);
    }

    @PostMapping("/add")
    @Operation(summary = "添加员工")
    public Result add(@RequestBody Employee  employee) {
        if(employee.getPassword()== null||employee.getPassword().trim().length()==0) {
            employee.setPassword("123456");
        }
        employee.setCreateTime(LocalDateTime.now());
        employeeService.save(employee);
        return Result.buildSuccess();
    }
    @PostMapping("/update")
    @Operation(summary = "更新员工")
    public Result update(@RequestBody Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return Result.buildSuccess();
    }
    @GetMapping("/delete")
    @Operation(summary = "删除员工")
    public Result delete(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        employeeService.removeById(id);
        return Result.buildSuccess();
    }
    @PostMapping("/list")
    @Operation(summary = "查询员工列表")
    public Result list(@RequestBody MEmployeeQueryDto employeeQueryDto) {
        Page<MEmployeeListVo> resultPage = employeeService.findEmployeeByQueryDto(employeeQueryDto);
        return Result.buildSuccess(resultPage.getRecords(), resultPage.getTotal());
    }

}

